package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.po.DistrictSimplePO;
import cn.tedu.tmall.front.mall.dao.cache.IDistrictCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IReceiverAddressRepository;
import cn.tedu.tmall.front.mall.pojo.entity.ReceiverAddress;
import cn.tedu.tmall.front.mall.pojo.param.ReceiverAddressAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IReceiverAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ReceiverAddressServiceImpl implements IReceiverAddressService {

    @Autowired
    private IReceiverAddressRepository receiverAddressRepository;
    @Autowired
    private IDistrictCacheRepository districtCacheRepository;

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, ReceiverAddressAddNewParam receiverAddressAddNewParam) {

        // 获取省编码
        DistrictSimplePO province = districtCacheRepository.getByCode(receiverAddressAddNewParam.getProvinceCode());
        // 获取市编码
        DistrictSimplePO city = districtCacheRepository.getByCode(receiverAddressAddNewParam.getCityCode());
        if (province == null || city == null) {
            String message = "添加收货地址失败，选择的地区数据有误！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        ReceiverAddress receiverAddress = new ReceiverAddress();
        BeanUtils.copyProperties(receiverAddressAddNewParam, receiverAddress);

        receiverAddress.setUserId(currentPrincipal.getId());
        receiverAddress.setProvince(province.getName());
        receiverAddress.setCity(city.getName());
        if (StringUtils.hasText(receiverAddressAddNewParam.getAreaCode())) {
            DistrictSimplePO area = districtCacheRepository.getByCode(receiverAddressAddNewParam.getAreaCode());
            receiverAddress.setArea(area.getName());
        }

        Integer isDefault = receiverAddressAddNewParam.getIsDefault();
        if (isDefault.equals(DEFAULT_STATE_ON)) {
            receiverAddressRepository.updateNonDefaultByUser(currentPrincipal.getId());
        }

        int rows = receiverAddressRepository.insert(receiverAddress);
        if (rows != 1) {
            String message = "添加收货地址失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public ReceiverAddressStandardVO getStandardById(CurrentPrincipal currentPrincipal, Long id) {
        // 核心业务逻辑
        ReceiverAddressStandardVO queryResult = receiverAddressRepository.getStandardById(id);

        // 判断根据ID查询的数据是否存在
        if (queryResult == null) {
            String message = "查询收货地址失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        // 判断根据ID查询到的数据是否归属当事人
        if (!queryResult.getUserId().equals(currentPrincipal.getId())) {
            String message = "查询收货地址失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        return queryResult;
    }

    @Override
    public List<ReceiverAddressListItemVO> listByUser(CurrentPrincipal currentPrincipal) {
        return receiverAddressRepository.listByUser(currentPrincipal.getId());
    }
}
